<?php
Class Product extends CI_Controller{
	function __construct(){
		parent::__construct();
		
		//insert visit
		$this->load->library('common');
		$this->common->insert_visit();
		
		$arr_material = $this->config->item("material");
		$is_show_best_sale_ao = $this->config->item("show_best_sale_aodais");
		$is_show_new_ao = $this->config->item("show_new_aodais");
		$this->load->model("md_type_product");
		$this->load->library('comment');
		
		$this->config->load('config', TRUE);
		
		$data = array(
			"materials" => $arr_material,
			"show_best_sale" => $is_show_best_sale_ao,
			"show_new"       => $is_show_new_ao,
			"type_aodai"=>$this->md_type_product->get_all("1"),
			"comments"   =>$this->comment->get_all_comment(),
			"hit_visit"  =>$this->common->get_all_visit()
		);
		
		//load language begin
		if ($this->session->userdata("language")){
	    	$this->lang->load("label",$this->session->userdata("language"));
	    	$data["language"]= $this->session->userdata("language");
	    	
	    }else{
	    	$this->lang->load("label");
	    	$data["language"]= $this->config->item("language");
	    }
		$this->load->helper('language');
		//load language end 
		
		
		//load calendar 
		$data["calendar"] =$this->get_calendar();
		//load calendar end
		
		$this->load->model("md_product");
		if ($is_show_best_sale_ao == true){
			$data["best_sale"] = $this->md_product->get_best_sale();
		}
		if ($is_show_new_ao == true){
			$data["new_aodai"] = $this->md_product->get_new_aodai();
		}
		
		$this->load->vars($data);
		$is_admin = 0;
		if ($this->session->userdata('login_info')){
			$user_info = $this->session->userdata('login_info') ;
			
			if ($user_info -> role == 1){
				$is_admin = 1;
			}
			$this->load->vars(array(
				'user_info'=>$user_info,
				'sodo_info'=>$this->session->userdata('sodo_info')
			
				)
			);
		}
		$this->load->vars(array('is_admin'=>$is_admin));
		
		
	}
	function add() {
		if ($this->session->userdata('is_admin')== 1){
			$arr_material = $this->config->item("material");
			$data = array(
				"materials" => $arr_material
			);
			$this->load->view("admin/create_product",$data)	;
		}else{
			//redirect trang loi
			redirect(base_url("/user/login"));
		}
		
	}
	function create(){
		if ($this->session->userdata('is_admin')== 1){
			$type = $_POST["type"];
			$price = $_POST["price"];
			$note = $_POST["note"];
			$image = $_POST["image"];
			$material = $_POST["material"];
			
					
			$this->load->model('md_product');
			$data = array(
				'type' => $type,
				'price'=>$price,
				'note'=>$note,
				'material' =>$material
					);
				
			$id= $this->md_product->insert($data);
			
			$this->load->model("md_type_product");		
			$arr_type = $this->md_type_product->get_one_by_id($type) ;
							
			$info_img_small =pathinfo($image);
			$img_big =$info_img_small['dirname']."/".substr($info_img_small['basename'], 6);
			$image = ".".$image;
			$img_big = ".".$img_big;
			
			$new_img = $arr_type[0]->name_short. $id .".".$info_img_small['extension'];
			//create folder contain image
			$folder_img = $id % 10 ;
			
			$img_big_name ="big_".$new_img;
			if (copy($image,"./assets/image/aodai/$folder_img/".$new_img) && copy($img_big,"./assets/image/aodai/$folder_img/".$img_big_name) ) {
			  //unlink($image);
			  //unlink($image);
			  $dir  = "./assets/temp/image/";
				foreach(glob($dir.'*.*') as $v){
					unlink($v);
				}
			}
			
			#update
			$this->load->model('md_product');
			$where = array(
				'id' => $id
			);
			$data_update =array(
				'image' => $new_img
			);
			$this->md_product->update($data_update,$where);
			
			$arr = array(
				'id' => $id
			);
			
			echo json_encode($arr);
		}else{
			//redirect trang loi
			redirect(base_url("/user/login"));
		}
	}
	function view($id){
		$this->load->model("md_product");
		$ret = $this->md_product->get_one($id);
		if ($this->session->userdata('is_admin')== 1){			
			$this->load->view("admin/view_product",$ret[0]);
		}else{
			$this->load->view("product/view",$ret[0]);
		}
	}
	function edit($id){
		if ($this->session->userdata('is_admin')== 1){
			$this->load->model("md_product");
			$ret = $this->md_product->get_one($id);
			$arr_material = $this->config->item("material");
			
			$data =array(
				"info" =>$ret[0],
				"type" =>"edit",
			"materials" => $arr_material
			);
			$this->load->view("admin/edit_product",$data);
		}else{
			//redirect trang loi
			redirect(base_url("/user/login"));
		}
	}
	
	function update($id){
		if ($this->session->userdata('is_admin')== 1){
			$type = $_POST["type"];
			$price = $_POST["price"];
			$note = $_POST["note"];
			$image = $_POST["image"];
			$material = $_POST["material"];
					
			$this->load->model('md_product');
			$data = array(
				'type' => $type,
				'price'=>$price,
				'note'=>$note,
				'material' =>$material
					);
			$where = array(
				'id' => $id
			);
			$this->md_product->update($data,$where);
			
			$this->load->model("md_type_product");
			
			$arr_type = $this->md_type_product->get_one_by_id($type) ;
	
			if ($image !=""){
				$info_img_small =pathinfo($image);
				$img_big =$info_img_small['dirname']."/".substr($info_img_small['basename'], 6);
				
				$image = ".".$image;
				$img_big = ".".$img_big;
				
				$new_img = $arr_type[0]->name_short. $id .".".$info_img_small['extension'];
				//create folder contain image
				$folder_img = $id % 10 ;
				
				$img_big_name ="big_".$new_img;
				if (copy($image,"./assets/image/aodai/$folder_img/".$new_img) && copy($img_big,"./assets/image/aodai/$folder_img/".$img_big_name) ) {
				 // unlink($image);
				 $this->remove_file_temp();
				}
				
				#update
				$this->load->model('md_product');
				$where = array(
					'id' => $id
				);
				$data_update =array(
					'image' => $new_img
				);
				$this->md_product->update($data_update,$where);
			}
			echo json_encode(array('status'=>'ok'));
		}else{
			//redirect trang loi
			redirect(base_url("/user/login"));
		}
		
		//redirect("/product/view/$id");
	}
	function search(){
		$type = $this->input->get("type");
		$material = $this->input->get("material");
		$start = $this->input->get("per_page");//isset($this->input->get("per_page")) ? $this->input->get("per_page") : 0;
		$limit = 10;
		
		if ($start == ""){
			$start = 0;
		}
		
		
		$where = array(
			'type' => $type,
			'material' =>$material
		);	
			
		$this->load->model('md_product');
		$data= $this->md_product->list_search($where,$limit,$start);
		$arr_material = $this->config->item("material");
		$result = array(
			"data"=>$data['result'],
			'materials' =>$arr_material,
			'type' =>$type,
			'material' =>$material
		);
		
		// generate pagination
		$this->load->library('pagination');
		$config['base_url'] = base_url('/product/search')."?test=test&type=$type&material=$material";

		$config['page_query_string'] = TRUE;
		$config['total_rows'] = $data['total'];
		$config['per_page'] = $limit;		
		
		$this->pagination->initialize($config);
		$result['str_paging'] = $this->pagination->create_links();
				
		if ($this->session->userdata('is_admin')== 1){
			$this->load->view("admin/list",$result)	;
		}else{
			$this->load->view("list_product",$result)	;
		}
			
		
	}
	
	function home(){
		
		//get all type ao dai
		$this->load->model("md_type_product");		
		$arr_type = $this->md_type_product->get_all("") ;
		
		
		$data_all = array();
		foreach ($arr_type as $type){
			$data = array();
			
			$data['id'] = $type->id;
			$data['name'] = $type->name;
			$data['name_eng'] = $type->name_eng;
			
			$this->load->model("md_product");
			$ret = $this->md_product->list_by_type($type->id,3);
			if($ret){
				$data['data'] = $ret;
				array_push($data_all,$data);
			}
			
		}
		$info =array(
			'info' => $data_all
		);
		$this->load->view("home",$info);
	}
	function test(){
		$this->load->library('fb_connect');
		$this->fb_connect->user_id; // returns the logged in users id
		$this->fb_connect->user; // returns an object containing all the user data.
		
	if($this->fb_connect->fbSession) {

		// logged in just fine
		echo "da login";
	
	} else  {
	
		//Not Logged in
	
	}
	}
	function delete(){
		if ($this->session->userdata('is_admin')== 1){
		 	$ids = $this->input->post("ma_aodai");	
			$this->load->model("md_product");
			$this->md_product->delete($ids);
		}else{
			redirect(base_url("/user/login"));
		}
	}
	function remove_file_temp(){
		$files = glob('./assets/temp/image/*'); // get all file names
		foreach($files as $file){ // iterate files
		  if(is_file($file))
		    unlink($file); // delete file
		}
				
	}
	function delete_theloai(){
		if ($this->session->userdata('is_admin')== 1){
			$this->load->model("md_type_product");
			$ids=$this->input->post("id");
			
			$this->md_type_product->delete($ids);
			
			$arr['info'] = $this->md_type_product->get_all();
			$this->load->view("admin/theloai/theloai_list",$arr);
		}else{
			redirect(base_url("/user/login"));
		}
	}
	function theloai(){
		if ($this->session->userdata('is_admin')== 1){
			$this->load->model("md_type_product");
			if($_POST){
				$theloai = $_POST['type'];
				$name=$_POST['name'];
				$name_short=$_POST['name_short'];
				$id=$_POST['id'];
				$info= array(
						'name'=>$name,
						'name_short'=>$name_short
					);
				if ($theloai ==  "create"){
					$id = $this->md_type_product->insert($info);
				}else{
					$this->md_type_product->update($info,$id);
				}
				
				$arr['info'] = $this->md_type_product->get_all();
				$this->load->view("admin/theloai/theloai_list",$arr);
			}else{
				$arr['info'] = $this->md_type_product->get_all("");
				$this->load->view("admin/theloai/theloai_create",$arr);
			}
		}else{
			redirect(base_url("/user/login"));
		}
		
	}
	function get_calendar(){
		$this->lang->load('calendar', $this->session->userdata("language"));
		$template = '

		   {table_open}<table  cellpadding="0" cellspacing="0" class="calendar" >{/table_open}
		
		   {heading_row_start}<tr>{/heading_row_start}
		
		   {heading_previous_cell}<th><a href="{previous_url}">&lt;&lt;</a></th>{/heading_previous_cell}
		   {heading_title_cell}<th colspan="{colspan}">{heading}</th>{/heading_title_cell}
		   {heading_next_cell}<th><a href="{next_url}">&gt;&gt;</a></th>{/heading_next_cell}
		
		   {heading_row_end}</tr>{/heading_row_end}
		
		   {week_row_start}<tr>{/week_row_start}
		   {week_day_cell}<td>{week_day}</td>{/week_day_cell}
		   {week_row_end}</tr>{/week_row_end}
		
		   {cal_row_start}<tr>{/cal_row_start}
		   {cal_cell_start}<td>{/cal_cell_start}
		
		   {cal_cell_content}<a href="{content}">{day}</a>{/cal_cell_content}
		   {cal_cell_content_today}<div class="highlight"><a href="{content}">{day}</a></div>{/cal_cell_content_today}
		
		   {cal_cell_no_content}{day}{/cal_cell_no_content}
		   {cal_cell_no_content_today}<div class="highlight">{day}</div>{/cal_cell_no_content_today}
		
		   {cal_cell_blank}&nbsp;{/cal_cell_blank}
		
		   {cal_cell_end}</td>{/cal_cell_end}
		   {cal_row_end}</tr>{/cal_row_end}
		
		   {table_close}</table>{/table_close}
		';
			  
		$prefs = array (
			 'start_day'    => 'monday',
               //'show_next_prev'  => TRUE,
               //'next_prev_url'   => 'http://example.com/index.php/calendar/show/',
               
               'template'        => $template
             );

		$this->load->library('calendar', $prefs);

	
		return $this->calendar->generate();
	}
	function change_language(){
		$language = $_POST['type'];
		$this->session->set_userdata("language",$language);
		//$this->config->set_value('language', 'value', 'config');
	}

} 
?>